﻿#include"Smart_Ptr.h"
#include"Shared_Ptr.h"

namespace Cw
{
	double Divide(int a, int b)
	{
		// 当b == 0时抛出异常
		if (b == 0)
		{
			throw "Divide by zero condition!";
		}
		else
		{
			return (double)a / (double)b;
		}
	}

	void Func()
	{
		// 这⾥使⽤RAII的智能指针类管理new出来的数组以后，程序简单多了
		SmartPtr<int> sp1 = new int[10];
		SmartPtr<int> sp2 = new int[10];
		for (int i = 0; i < 10; i++)
		{
			sp1[i] = sp2[i] = i;
		} 
		int len, time;
		cin >> len >> time;
		cout << Divide(len, time) << endl;
	}
}

void Try_Catach()
{
	try
	{
		Cw::Func();
	}
	catch (const char* errmsg)
	{
		cout << errmsg << endl;
	}
	catch (const exception& e)
	{
		cout << e.what() << endl;
	}
	catch (...)
	{
		cout << "未知异常" << endl;
	}
}


struct Date
{
	int _year;
	int _month;
	int _day;
	Date(int year = 1, int month = 1, int day = 1)
		:_year(year)
	    , _month(month)
		, _day(day)
	{}
	~Date()
	{
		cout << "~Date()" << endl;
	}
};


int main()
{
	//Try_Catach();

	Cw ::Shared_Ptr<Date> sp1(new Date);
	Cw ::Shared_Ptr<Date> sp2(sp1);
	Cw ::Shared_Ptr<Date> sp3(sp2);
	cout << sp1.use_count() << endl;
	sp1->_year++;
	cout << sp1->_year << endl;
	cout << sp2->_year << endl;
	cout << sp3->_year << endl;

	return 0;
}